package com.yq.bdm.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yq.bdm.controller.common.AbstractController;
import com.yq.bdm.entity.RolePermissionEntity;
import com.yq.bdm.service.RolePermissionService;
import com.yq.bdm.utils.PageUtils;
import com.yq.bdm.utils.ResultMap;


/**
 * 角色权限表
 * @author yuq
 * @date 2017-07-21 16:35:14
 */
@Controller
@RequestMapping("/rolepermission")
public class RolePermissionController  extends AbstractController   {

	@Autowired
	private RolePermissionService rolePermissionService;
	
	@RequestMapping("/index.html")
	public String list(){
		return "admin/rolepermission/rolepermission.html";
	}
	
	/**
	 * 列表
	 */
	@ResponseBody
	@RequestMapping("/list")
	public ResultMap list(
				String id , 				String roleId , 				String permissionId 		){
	
		Map<String, Object> map = new HashMap<>();
		//查询条件
				    map.put("id", id);
				    map.put("roleId", roleId);
				    map.put("permissionId", permissionId);
				
		//获取分页参数，添加到查询请求参数中
		getPage(map);

		// 查询列表数据
		List<RolePermissionEntity> rolePermissionList = rolePermissionService.queryList(map);
		int total = rolePermissionService.queryTotal(map);
		PageUtils pageUtil = new PageUtils(rolePermissionList, total,map);
		return ResultMap.ok().put("page", pageUtil);
	}
	
	/**
	 * 跳转到新增页面
	 */
	@RequestMapping("/goAdd")
	public String goAdd() {
		return "admin/rolepermission/add.html";
	}
	
	/**
	 * 跳转到编辑页面
	 */
	@RequestMapping("/goEdit/{id}")
	public String goEdit(@PathVariable("id") Long  id) {
		//设置当前control的访问url，edit页面需要使用
		request.setAttribute("namespaceUrl", request.getContextPath()+ "/rolepermission");
		RolePermissionEntity rolePermission = rolePermissionService.queryObject(id);
		request.setAttribute("rolePermission", rolePermission);
		return "admin/rolepermission/edit.html";
	}
	
	/**
	 * 信息
	 */
	@ResponseBody
	@RequestMapping("/info/{id}")
	public ResultMap info(@PathVariable("id") Long id){
		RolePermissionEntity rolePermission = rolePermissionService.queryObject(id);
		
		return ResultMap.ok().put("rolePermission", rolePermission);
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@RequestMapping("/save")
	public ResultMap save(@RequestBody RolePermissionEntity rolePermission){
		rolePermissionService.save(rolePermission);
		
		return ResultMap.ok();
	}
	
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	public ResultMap update(@RequestBody RolePermissionEntity rolePermission){
		rolePermissionService.update(rolePermission);
		
		return ResultMap.ok();
	}
	
	/**
	 * 删除
	 */
	@ResponseBody
	@RequestMapping("/delete")
	public ResultMap delete(@RequestBody Long[] ids){
		rolePermissionService.deleteBatch(ids);
		
		return ResultMap.ok();
	}
	
	/**
	 * 删除成功后，查询列表的数据
	 */
	@ResponseBody
	@RequestMapping("/deleteAfterList")
	public ResultMap deleteAfterList(@RequestBody Long[] ids) {
		rolePermissionService.deleteBatch(ids);
		
		Map<String, Object> map = new HashMap<>();
		// 获取分页参数，添加到查询请求参数中
		getPage(map);
		// 查询列表数据
		List<RolePermissionEntity> rolePermissionList = rolePermissionService.queryList(map);
		int total = rolePermissionService.queryTotal(map);
		PageUtils pageUtil = new PageUtils(rolePermissionList, total, map);
		return ResultMap.ok().put("page", pageUtil);
	}
}
